package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.detail;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;

/**
 * 作业详情-试题类
 */
@CrossOrigin
@RestController
public class HomeworkDetailQues0134Controller extends YxyApiBaseController {

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    ElasticQuesService elasticQuesService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/detail/ques0134/find", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String classId, //
            @RequestParam String taskId, //
            RespModel respModel) throws ParseException {

        // 获得作业试题的 单选题 OR 多选题 OR 填空题 OR 简答题
        List<HomeworkTaskQues> homeworkTaskQuesLst = homeworkTaskQuesRepository
                .findByTaskIdAndQuesTypeInOrderByOrderAsc(taskId, new String[] { "0", "1", "3", "4" });

        // 获取 es 试题
        List<String> quesIds = new ArrayList<String>();
        homeworkTaskQuesLst.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        List<ElasticYxyYxyQuesResource> questions = new ArrayList<ElasticYxyYxyQuesResource>();
        if (quesIds.size() > 0) {
            questions = elasticQuesService.findByQuesIdIn(quesIds);
        }

        // return
        List<RespModel> models = new ArrayList<RespModel>();
        for (HomeworkTaskQues homeworkTaskQues : homeworkTaskQuesLst) {
            RespModel model = new RespModel();
            model.quesId = homeworkTaskQues.getQuesId();
            model.ques = null;
            questions.forEach(question -> {
                if (question.getId().equals(model.quesId)) {
                    model.ques = question;
                }
            });
            model.order = homeworkTaskQues.getOrder();
            model.questionType = homeworkTaskQues.getQuesType();
            models.add(model);
        }
        return models;
    }

    /** 响应 **/
    protected static class RespModel extends YxyApiRespModel {
        public Object ques; // 试题 Object
        public String quesId; // 试题Id
        public Integer order; // 试题序号
        public String questionType; // 试题类型
        public boolean isShow = false; // 是否展示
    }
}